<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Session;
use DB;
use Config;
use Image;
use App\Http\Controllers\Controller;
use Illuminate\Http\UploadedFile;

class PropertyController extends Controller
{
    public static  $dir_img = [];
    public function index(){
        session_start();
        if (Session::has('property_id') && Session::has('property_name')) {
            return view('admin/property.index');
        }
    }
    public function house_add(){
        session_start();
        if (Session::has('property_id') && Session::has('property_name')) {
            $res = DB::table('sale_user')->get();
            return view('admin/property.houseadd',['data'=>$res]);
            }
    }
    public function upload(Request $request){
        //接收上传的文件
        $file = $request->file('photo');
        if(!empty($file)) {
            // 获取文件路径
            $transverse_pic = $file->getRealPath();
            // public路径
            $path = public_path('uploads');
            // 获取后缀名
            $postfix = $file->getClientOriginalExtension();
            // 拼装文件名
            $fileName = md5(time().rand(0,10000)).'.'.$postfix;
            self::$dir_img = $fileName;
            // 移动
            if(!$file->move($path,$fileName)){
                return response()->json(['ServerNo' => '400','ResultData' => '文件保存失败']);
            }
            // 这里处理 数据库逻辑
            /**
             *Store::uploadFile(['fileName'=>$fileName]);
             **/
            return response()->json(['code'=>0,'ServerNo' => '200','ResultData' => $fileName]);

        }

    }
    public function house_add_do(Request $request){
        session_start();
        if (Session::has('gm_name')) {
            $res = Session::all();
            $house_room = $request->house_room;
            $house_region = $request->house_region;
            $house_village_name = $request->house_village_name;
            $house_address = $request->house_address;
            $house_unit_num = $request->house_unit_num;
            $house_layer_num = $request->house_layer_num;
            $house_price = $request->house_price;
            $house_size = $request->house_size;
            $is_television = $request->is_television;
            $is_air_conditioning = $request->is_air_conditioning;
            $is_refrigerator = $request->is_refrigerator;
            $house_text = $request->house_text;
            $sale_id = $request->sale_id;
            $house_img_one = $request->house_img_one;
            $sale_property_id = $res['id'];
            $data = DB::table('house')->insert([
                'house_room' => $house_room,
                'house_region' => $house_region,
                'house_village_name' => $house_village_name,
                'house_address' => $house_address,
                'house_unit_num' => $house_unit_num,
                'house_layer_num' => $house_layer_num,
                'house_price' => $house_price,
                'house_size' => $house_size,
                'is_television' => $is_television,
                'is_air_conditioning' => $is_air_conditioning,
                'is_refrigerator' => $is_refrigerator,
                'house_text' => $house_text,
                'sale_id' => $sale_id,
                'property_id' =>$sale_property_id,
                'house_img_one' =>$house_img_one,
            ]);
            if($data == 1){
                return 1;
            }else{
                return 0;
            }

        }

    }
    public function review(Request $request){
        session_start();
        if (Session::has('property_id') && Session::has('property_name')) {
            $res = Session::all();
            $sale_property_id = $res['property_id'];
            $data = DB::table('sale_user')->where([
                ['property_id', '=', $sale_property_id],
            ])->get();
            return view('admin/property.review',['list'=>$data]);
        }else{
            return view('admin/login.index');
        }
    }
    public function review_res(Request $request){
         $sale_status = $request->sale_status;
         $sale_id = $request->sale_id;
         echo $sale_status;
         if($sale_status == 1){
             $res = DB::table('sale_user')
                 ->where('sale_id','=', $sale_id)
                 ->update(['sale_status' => 0]);
         }else{
             $res = DB::table('sale_user')
                 ->where('sale_id', $sale_id)
                 ->update(['sale_status' => 1]);
         }
        if($res){
            return 1;
        }else{
            return 0;
        }

    }
}